package gg.com.toolbox.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import gg.com.toolbox.R;
import gg.com.toolbox.activity.NotepadActivity;

/**
 * 弹出窗口
 * Created by gaoge on 2017/10/12.
 */

public class MyAlertDialog {

    private static AlertDialog ad;
    private EditText editText1 = null;
    private EditText editText2 = null;
    private EditText editText3 = null;
    private EditText editText4 = null;
    private TextView textView1 = null;
    private TextView textView2 = null;
    private TextView textView3 = null;

    /**
     * 添加账号信息弹出框
     *
     * @param context
     * @param layout
     */
    public AlertDialog addUserNameDialog(Context context, int layout, int i) {
        ad = new AlertDialog.Builder(context, R.style.dialog).create();
        ad.setCancelable(true);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        assert window != null;
        window.setContentView(layout);
        //获取一个LayoutParams对象用于后面设置回去
        WindowManager.LayoutParams lp = window.getAttributes();
        //调整明暗度，float值，完全透明不变暗是0.0f，完全变暗不透明是1.0f
        lp.dimAmount = 0.0f;
        //必须要设置回去
        window.setAttributes(lp);
        //根据谷歌文档，给对应的Window添加FLAG_DIM_BEHIND标志位，dimAmount值才有效。
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        //只用下面这一行弹出对话框时需要点击输入框才能弹出软键盘
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        if (i == 0) {
            //加上下面这一行弹出对话框时软键盘随之弹出
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
        //得到控件
        editText1 = (EditText) window.findViewById(R.id.title_edit);
        editText2 = (EditText) window.findViewById(R.id.username_edit);
        editText3 = (EditText) window.findViewById(R.id.password_edit);
        editText4 = (EditText) window.findViewById(R.id.remarks_edit);
        textView1 = (TextView) window.findViewById(R.id.cancel_text);
        textView2 = (TextView) window.findViewById(R.id.confirm_text);
        return ad;
    }

    /**
     * 删除账号信息弹出框
     *
     * @param context
     * @param layout
     */
    public AlertDialog deleteUserNameDialog(Context context, int layout) {
        ad = new AlertDialog.Builder(context, R.style.dialog).create();
        ad.setCancelable(true);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        assert window != null;
        window.setContentView(layout);
        //获取一个LayoutParams对象用于后面设置回去
        WindowManager.LayoutParams lp = window.getAttributes();
        //调整明暗度，float值，完全透明不变暗是0.0f，完全变暗不透明是1.0f
        lp.dimAmount = 0.0f;
        //必须要设置回去
        window.setAttributes(lp);
        //根据谷歌文档，给对应的Window添加FLAG_DIM_BEHIND标志位，dimAmount值才有效。
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        //得到控件
        textView1 = (TextView) window.findViewById(R.id.cancel_text);
        textView2 = (TextView) window.findViewById(R.id.confirm_text);
        textView3 = (TextView) window.findViewById(R.id.data);
        return ad;
    }

    //提供EditText1控件
    public EditText getEditText1() {
        return editText1;
    }

    //提供EditText2控件
    public EditText getEditText2() {
        return editText2;
    }

    //提供EditText3控件
    public EditText getEditText3() {
        return editText3;
    }

    //提供EditText4控件
    public EditText getEditText4() {
        return editText4;
    }

    //提供TextView1控件
    public TextView getTextView1() {
        return textView1;
    }

    //提供TextView2控件
    public TextView getTextView2() {
        return textView2;
    }

    //提供TextView3控件
    public TextView getTextView3() {
        return textView3;
    }

    //关闭
    public void dismissAd() {
        ad.dismiss();
        ad = null;
    }
}
